<?php

namespace App\Console;

use App\Console\Commands\Clear\Statistics;
use App\Console\Commands\Data\AreaCommand;
use App\Kuafu\Order\Command\ShopDayStatistics;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Kuafu\IpWhite\Command\FlushIps;
use Kuafu\IpWhite\Command\ForceLoginOut;
use Kuafu\IpWhite\Command\TotalLists;
use Kuafu\IpWhite\Command\UserLists;
use KuafuComposer\ApiTime\Command\ApiTimeBatchPushCommand;
use KuafuComposer\ApiTime\Command\ApiTimeGetSlowDetailCommand;
use KuafuComposer\ApiTime\Command\ApiTimePushCommand;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        ShopDayStatistics::class,
        AreaCommand::class,
        Statistics::class,
        ApiTimePushCommand::class,
        ApiTimeBatchPushCommand::class,
        ApiTimeGetSlowDetailCommand::class,
//        ForceLoginOut::class,
//        UserLists::class,
//        TotalLists::class,
//        FlushIps::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
//         $schedule->command('inspire')->everyFiveMinutes();

        // 日结单飞书推送
//        $schedule->command('order:shop-day-statistics')->dailyAt("23:00");
        // 订单自查
        $schedule->command('order:sync-abnormality-order --d=true')->dailyAt("05:55");

        // 外卖订单日数据统计
        $schedule->command('waimai:batch-query-exec --d=true')->dailyAt("06:10");

        $schedule->command('kuafu:activity:update_status')->everyMinute();

        // 外卖订单状态自查
        $schedule->command('waimai:check-status')->dailyAt("09:30");

        // 观远数据同步 - 外卖
        $schedule->command('guanyuan:wmSyncDishDetail --d=true')
                 ->dailyAt("02:10")
                 ->appendOutputTo(storage_path('logs/kernel_guanyuan_capture_wmSyncDishDetail-') . date('Ymd') . '.log')
                 ->runInBackground();
        // 观远数据同步 - 堂食
        $schedule->command('guanyuan:kfSyncDishDetail --d=true')
                 ->dailyAt("01:00")
                 ->appendOutputTo(storage_path('logs/kernel_guanyuan_kfSyncDishDetail-') . date('Ymd') . '.log')
                 ->runInBackground();

        //券包同步到观远
        $schedule->command('guanyuan:packageCouponSyncDishDetail --d=true')->dailyAt("02:35");

        // 券包订单，定期关闭支付
        $schedule->command('coupon-package:close-non-pay-order')->everyMinute();

        // 批量同步支付统计明细
        $date = date("Ymd", strtotime("-2 day"));
        $schedule->command("pay-init-union 0 {$date} 0")->dailyAt("06:00");
        $schedule->command('pay-init-union')->dailyAt("06:00");

        // 自动刷新外卖鉴权Token
        $schedule->command('waimai:refresh-token')->everyTenMinutes();

        // 推送昨日慢接口日志
        $schedule->command('api-time:push-slow-api --fsRobot=' . SendFs::ROBOT_FS_API_TIME)->dailyAt("09:00");

        // 下载对账文件
        $schedule->command('pay:get-check-file')->dailyAt('10:00');

        // 定期更新三方外卖承诺出餐时间
        $schedule->command('waimai:refresh-promise-time')->everyThirtyMinutes();

        // 凌晨4点更新摄像头
        $schedule->command('tools:import-ylk')->dailyAt('04:00');

        // 每隔 5 分钟更新摄像头
        $schedule->command('tools:refresh-yl-camera-channel')->everyFiveMinutes();

        // MQTT - 清理日志
        $schedule->command('mqtt:removeLog')->dailyAt("04:01");

        // 清理数据
        $schedule->command('data:timingDeleteData')->dailyAt("04:05");

        // 飞书用户同步 8-22点每小时执行一次
        $schedule->command('fs:SynchronizeUserinfo')->cron('0 8-22 * * *');

        // 每天 4:30 刷新门店分公司
        $schedule->command('tools:refresh-shop-company')->dailyAt("04:30");

        // 每天 3:30 同步前一天的客流数据
        $schedule->command('tools:sync-ylk-passenger-flow')->dailyAt("03:30");

        // 1点同步 QA 外卖记录
//        $schedule->command('mybank:SyncQaOrder')->dailyAt("01:00");

        // 每天 6 点删除脏数据
        $schedule->command('order:street-sweeper-deleted')->dailyAt("05:50");
        $schedule->command('order:street-sweeper-tester')->dailyAt("06:00");

        // 更新门店风云榜
        $schedule->command('shop:shop_business_update')->dailyAt("09:30");

        // 每周3，凌晨3点。备份waimai_callback_logs
//        $schedule->command('waimai:bak')->weeklyOn("3", "03:00");
        // 导出文件下载中心 - 30天自动删除历史记录及资源
        $schedule->command('clear:export-downloads-resource')->dailyAt("03:30");

        // 美团外卖同步
        $schedule->command('mybank:SyncMeiTuanTakeAwayOrderDetail')->dailyAt('10:20');
        // 美团外卖同步 - Again
        $schedule->command('mybank:SyncMeiTuanTakeAwayOrderDetail')->dailyAt('10:50');

        // 每月10号凌晨3点清理日志
        $schedule->command('waimai:log-clear 1')->monthlyOn(10, '02:40');

        // 每小时 17 分更新前 1 小时的流纬度数据
        $schedule->command('tools:clean-shop-monitor-data')->hourlyAt(17);

        // 每天02:17 分清洗 前1 天 的客流纬度数据
        $schedule->command('tools:clean-shop-monitor-data --cycle=yesterday')->dailyAt('06:01')->runInBackground();

        // 简易 客流日常预警
        $schedule->command('tools:check-shop-monitor-data')->dailyAt('10:10')->runInBackground();

        // 检查elm token，自动修复
        // $schedule->command('waimai:check-token')->everyMinute();

        // 每天凌晨3点生成店铺分销分佣记录
        $schedule->command('shop:distributionBillRecordDaily')->dailyAt('03:00');

        // 饿了么未正常响应的请求处理
        $schedule->command('waimai:elm-pull-err-callback 1')->everyMinute()->runInBackground();

        // 每 4 小时同步 抖音来客商品适用门店
        $schedule->command('tools:douyin:refresh-product')
                 ->hourly()
                 ->appendOutputTo(storage_path('logs/kernel/tools/douyin_refresh-product') . date('Ymd') . '.log')
                 ->runInBackground();

        /**
         * 网商+任务 任务组
         */
        $this->mybankTask($schedule);

        // 每小时 初始化门店简称
        $schedule->command('initShortName')->hourly()->runInBackground();

        // 每月备份订单数据库
        $lastMonth = date('Ym', strtotime('-1 month'));
        $schedule->command("bak:order-month {$lastMonth}")->monthlyOn(20, '01:30')->runInBackground();

        // 每天12点01 每天同步门店属性-门店状态 (tiger每天早上9点05同步观远门店列表)
        $schedule->command('shop:syncShopAttributeDay')->dailyAt('12:01')->runInBackground();

        // 每天清理separate_time_logs
        $schedule->command('order:clear-separate-time-log')->dailyAt('02:10')->runInBackground();

        // 商家首页数据统计
        $schedule->command('user:merchant-user-active')->dailyAt('08:50');

        //健康证状态检查
        $schedule->command('shop:check_health_card')->dailyAt('01:00');

        // 同步新增门店档案逻辑
        $schedule->command('shop:sync_new_shop_record')->dailyAt('01:15');

        // 每天批量获取摄像头状态并更新到表 8-23点 每小时定时脚本
        $schedule->command('tools:batch-camera-status')->cron('10 8-23 * * *')->runInBackground();

        // 每小时更新门店统管数据，新增门店更新
        $schedule->command('shop:refresh-unified-management')->hourlyAt(8)->runInBackground();

        // 删除多余的客流数据
        $schedule->command('tools:delete-shop-monitor-data')->dailyAt('04:55')->runInBackground();

    }

    /**
     * 网商+分账 任务
     *
     * @return void
     */
    protected function mybankTask(Schedule $schedule)
    {
        // 每天 08:01 获取饿了么账单数据
        $schedule->command('waimaiIncome:eleme')
                 ->dailyAt("08:01")
                 ->appendOutputTo(storage_path('logs/kernel/tools/waimaiIncome-eleme') . date('Ymd') . '.log')
                 ->runInBackground();

        // 账户对账+批次创建
        $schedule->command('mybank:create-batch-capital-clearing')
                 ->dailyAt('11:30')
                 ->appendOutputTo(storage_path('logs/kernel/tools/create-batch-capital-clearing') . date('Ymd') . '.log')
                 ->runInBackground();

        // 堂食同步
        $schedule->command('mybank:syncFuyouBillCommand')
                 ->dailyAt('10:00')
                 ->appendOutputTo(storage_path('logs/kernel/tools/syncFuyouBillCommand') . date('Ymd') . '.log')
                 ->runInBackground();

        // 堂食订单同步至网商
        $schedule->command('mybank:rms-order-bill-pay')
                 ->dailyAt('10:29')
                 ->appendOutputTo(storage_path('logs/kernel/tools/rms-order-bill-pay') . date('Ymd') . '.log')
                 ->runInBackground();

        // 每天 2 点 10 分刷新实收, 总实收
        $schedule->command('mybank:refresh-shop-income')
                 ->dailyAt('02:11')
                 ->appendOutputTo(storage_path('logs/kernel/tools/refresh-shop-income') . date('Ymd') . '.log')
                 ->runInBackground();

        // 每天 12 点 30 分 解冻商户余额
        $schedule->command('mybank:unfreezeMerchantBalance')
                 ->dailyAt('12:30')
                 ->appendOutputTo(storage_path('logs/kernel/tools/unfreeze-merchant-balance') . date('Ymd') . '.log')
                 ->runInBackground();

    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');
        // 额外载入app/Kuafu里面的命令，会导致php artisan变慢，好处就不需要手动维护$this->commands了
        $this->load(app_path('Kuafu'));
        require base_path('routes/console.php');
    }
}
